package com.pms.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * 首页
 * @author huyinsong
 *
 */
@Controller
public class IndexAction {

	@RequestMapping(value="/index.pms")
	public String index(HttpServletRequest req,ModelMap mm){
		Map<String,Cookie> map=getCookies(req);
		if(map.containsKey("permission")){
			if("yes".equals(map.get("permission").getValue())){
				mm.addAttribute("userid",map.get("userid").getValue());
				mm.addAttribute("username",map.get("username").getValue());
				return "index";
			}
		}
		return "redirect:login.pms";
		
	}
	
	private Map<String,Cookie> getCookies(HttpServletRequest req){
		Map<String,Cookie> map=new HashMap<String,Cookie>();
		Cookie[] cookies=req.getCookies();
		for(int i=0;i<cookies.length;i++){
			Cookie c=cookies[i];
			map.put(c.getName(), c);
		}
		return map;
	}
}
